package com.soul.soulservice.email.imp;
import com.soul.soulcommon.dto.ResetPwd;
import com.soul.soulcommon.result.Result;
import com.soul.soulcommon.utils.RedisUtils;
import com.soul.soulservice.email.ResetPwdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ResetPwdServiceImp implements ResetPwdService {
    @Autowired
    private RedisUtils utils;
    //获取对应邮箱的验证码
    @Override
    public String getEmail(String email){
        try {
            return (String) utils.get(email, String.class);
        }catch (RuntimeException e){
            log.error("redis获取验证码异常：" + e.getMessage());
            return null;
        }
    }
    //校验验证码
    @Override
    public boolean checkEmail(String email,String code){
        String redisCode = getEmail(email);
        log.info(email + "正在校验中");
        return redisCode != null && redisCode.equals(code);
    }

    @Override
    public Result checkCode(ResetPwd resetPwd) {
        String email = resetPwd.getEmail();
        String code = resetPwd.getCode();
        String twoPassword = resetPwd.getTwoPassword();
        //判断两次密码是否一致，不一致直接不进行后面的逻辑
        if(!twoPassword.equals(resetPwd.getPassword())){
            return Result.error("两次密码不一致");
        }
        //假逻辑，真实的需要修改数据库
        boolean b = checkEmail(email, code);
        if(b){
            return Result.success("修改密码成功");
        }else{
            return Result.success("修改密码失败");
        }
    }
}
